package com.qnvip.sbd.util;

import com.alibaba.fastjson.JSONObject;
import org.apache.commons.lang3.StringUtils;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

public class JsonResult {
	private Object values;
	private String errcode = "0";
	private String errmsg = StringUtils.EMPTY;
	
	public static JsonResult N(String errcode, String errmsg) {
		return new JsonResult().setErrcode(errcode).setErrmsg(errmsg); 
	}
	
	public static JsonResult N(String[] errInfo) {
		return new JsonResult().setErrcode(errInfo[0]).setErrmsg(errInfo[1]);
	}
	
	public static JsonResult N(Object obj) {
		return new JsonResult().setValues(obj);
	}
	
	public JsonResult() {};
	
	public static JsonResult SUCCESS() {
		return  new JsonResult().setValues(StringUtils.EMPTY);
	}
	
	@SuppressWarnings("unchecked")
	public JsonResult(Object obj, String args) {
		if (StringUtils.isEmpty(args))
			this.values = obj;
		else {
			if (obj instanceof  List) {
				List<Map<String, Object>> list = new ArrayList<Map<String,Object>>();
				for (Object o : (ArrayList<Object>) obj) {
					Map<String, Object> map = BeanUtil.bean2Map(o, args);
					list.add(map);
				}
				this.values = list;
			} else
				this.values = BeanUtil.bean2Map(obj, args);
		}
	}
	
	public void setError(String[] errInfo) {
		this.errcode = errInfo[0];
		this.setErrmsg(errInfo[1]);
	}

	public String toJsonString() {
	    return JSONObject.toJSONString(this);
	}

	public String getErrcode() {
		return errcode;
	}

	public JsonResult setErrcode(String errcode) {
		this.errcode = errcode;
		return this;
	}

	public Object getValues() {
		return values;
	}

	public JsonResult setValues(Object values) {
		this.values = values;
		return this;
	}

	public String getErrmsg() {
		return errmsg;
	}

	public JsonResult setErrmsg(String errmsg) {
		this.errmsg = errmsg;
		return this;
	}
}
